package com.ice.framework.base

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.ice.framework.extend.setOnNotDoubleClickListener
import com.orhanobut.logger.Logger

abstract class BaseRecyclerAdapter<T,T1:ViewBinding> : RecyclerView.Adapter<BaseRecyclerAdapter.ViewHolder<T1>>() {

    var notifyDataSetChangedListener:(()->Unit)?=null
    set(value) {
        field=value
        notifyDataSetChanged()
    }

    //被点击的下标
    var selectPosition:Int=0
        set(value) {
            field=value
            notifyDataSetChanged()
        }

    var selectPositions:ArrayList<Int> = ArrayList()
        set(value){
            field=value
            notifyDataSetChanged()
        }

    var list=ArrayList<T>()
        set(value) {
            field=value
            notifyDataSetChanged()
            notifyDataSetChangedListener?.invoke()
        }

    //点击事件回调
    var onItemClickListener:((position:Int)->Unit)?=null
    set(value) {
        field=value
        notifyDataSetChanged()
    }

    abstract fun initViewBinding(context:Context,viewGroup: ViewGroup):T1

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder<T1>
            =ViewHolder(initViewBinding(viewGroup.context,viewGroup),viewGroup.context)

    override fun getItemCount(): Int=list.size

    override fun onBindViewHolder(viewHolder: ViewHolder<T1>, position: Int){
        viewHolder.itemView.setOnClickListener {
            selectPosition=position
            if(selectPositions.contains(position)){
                selectPositions.remove(position)
            }else{
                selectPositions.add(position)
            }
            onItemClickListener?.invoke(position)
        }
    }

    class ViewHolder<T1:ViewBinding>(val binding: T1,val context: Context): RecyclerView.ViewHolder(binding.root)
}
